<template>
  <div class="app-container calendar-list-container">

    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-input clearable class="filter-item" style="width: 200px;" placeholder="请输入服务项目编号" v-model="listQuery.goodsSn">
      </el-input>
      <el-input clearable class="filter-item" style="width: 200px;" placeholder="请输入服务项目名称" v-model="listQuery.name">
      </el-input>
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">查找</el-button>
      <el-button class="filter-item" type="primary" @click="handleCreate" icon="el-icon-edit">添加</el-button>
      <el-button class="filter-item" type="primary" :loading="downloadLoading" icon="el-icon-download" @click="handleDownload">导出</el-button>
    </div>

    <!-- 查询结果 -->
    <el-table size="small" :data="list" v-loading="listLoading" element-loading-text="正在查询中。。。" border fit highlight-current-row>

      <el-table-column type="expand">
        <template slot-scope="props">
          <el-form label-position="left" class="table-expand">
            <el-form-item label="宣传画廊">
              <img class="gallery" v-for="pic in props.row.gallery" :key="pic" :src="pic" />
            </el-form-item>
            <el-form-item label="服务项目介绍">
              <span>{{ props.row.brief }}</span>
            </el-form-item>
            <el-form-item label="服务项目单位">
              <span>{{ props.row.unit }}</span>
            </el-form-item>
            <el-form-item label="关键字">
              <span>{{ props.row.keywords }}</span>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>

      <el-table-column align="center" label="服务项目编号" prop="goodsSn">
      </el-table-column>

      <el-table-column align="center" min-width="100" label="名称" prop="name">
      </el-table-column>

      <el-table-column align="center" property="iconUrl" label="图片">
        <template slot-scope="scope">
          <img :src="scope.row.picUrl" width="40" />
        </template>
      </el-table-column>

      <el-table-column align="center" label="详情" prop="desc">
        <template slot-scope="scope">
          <el-dialog title="服务项目详情" :visible.sync="descDialogVisible">
            <div v-html="descDetail"></div>
          </el-dialog>
          <el-button type="primary" size="mini" @click="showDesc(scope.row.desc)">查看</el-button>
        </template>
      </el-table-column>

      <el-table-column align="center" label="库存" prop="counterPrice">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="showRepetory(scope.row.id)">查看</el-button>
        </template>
      </el-table-column>

      <el-table-column align="center" label="积分" min-width="100" prop="retailPrice">
      </el-table-column>

      <el-table-column align="center" label="是否新品" prop="isNew">
        <template slot-scope="scope">
          <el-tag :type="scope.row.isNew ? 'success' : 'error' ">{{scope.row.isNew ? '新品' : '非新品'}}</el-tag>
        </template>
      </el-table-column>

      <el-table-column align="center" label="是否在售" prop="isOnSale">
        <template slot-scope="scope">
          <el-tag :type="scope.row.isOnSale ? 'success' : 'error' ">{{scope.row.isOnSale ? '在售' : '未售'}}</el-tag>
        </template>
      </el-table-column>

      <el-table-column align="center" property="isCheck" label="审核状态">
        <template slot-scope="scope">
          <img :src="scope.row.isCheck === 0 ? weishenhe : scope.row.isCheck === 1 ? shenhezhong : scope.row.isCheck === 2 ? yitongguo : scope.row.isCheck === 3 ? weitongguo : ''" width="40" :title="scope.row.isCheck === 0 ? '未审核' : scope.row.isCheck === 1 ? '审核中' : scope.row.isCheck === 2 ? '已通过' : scope.row.isCheck === 3 ? '未通过' : ''" />
        </template>
      </el-table-column>

      <el-table-column align="center" label="操作" width="360" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-if="scope.row.isCheck === 0 || scope.row.isCheck === 3" type="primary" size="small" @click="handleCheck(scope.row)">提交审核</el-button>
          <el-button v-if="scope.row.isCheck >= 0" type="primary" size="small" @click="lookReason(scope.row)">审核详情</el-button>
          <el-button v-if="scope.row.isCheck !== 2" type="primary" size="small" @click="handleUpdate(scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div class="pagination-container">
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="listQuery.page" :page-sizes="[10,20,30,50]" :page-size="listQuery.limit" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>
    <!-- 库存详情 -->
    <el-dialog title="库存详情" width="800" :visible.sync="productVisible">
      <el-form :data="productForm" label-position="left">
        <el-form-item>
          <el-table size="small" :data="productForm" border fit highlight-current-row>
            <el-table-column align="center" label="服务项目规格" prop="specifications" />
            <el-table-column align="center" label="库存数量" prop="number" />
          </el-table>
        </el-form-item>
      </el-form>
    </el-dialog>

    <!-- 不通过原因 -->
    <el-dialog title="审核详情" width="800" :visible.sync="describeVisible">
      <el-form :data="goodsCheck" label-position="left">
        <el-form-item>
          <el-table size="small" :data="goodsCheck" border fit highlight-current-row>
            <el-table-column align="center" label="提交审核时间" prop="addTime" />
            <el-table-column align="center" label="审核通过时间" prop="transitTime" />
            <el-table-column align="center" label="审核状态" prop="isCheck" >
              <template slot-scope="scope">
                <el-tag :type="scope.row.isCheck == 0 ? 'info' : scope.row.isCheck == 1 ? '' : scope.row.isCheck == 2 ? 'success' : scope.row.isCheck == 3 ? 'danger' : 'warning'">
                  {{scope.row.isCheck == 0 ? '未审核' : scope.row.isCheck == 1 ? '审核中' : scope.row.isCheck == 2 ? '已通过' : scope.row.isCheck == 3 ? '未通过' : '未知状态'}}
                </el-tag>
              </template>
            </el-table-column>
            <el-table-column align="center" label="不通过原因" prop="describe" />
          </el-table>
        </el-form-item>
      </el-form>
    </el-dialog>

    <el-tooltip placement="top" content="返回顶部">
      <back-to-top :visibilityHeight="100"></back-to-top>
    </el-tooltip>

  </div>
</template>

<style>
.table-expand {
  font-size: 0;
}
.table-expand label {
  width: 100px;
  color: #99a9bf;
}
.table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
}
.gallery {
  width: 80px;
  margin-right: 10px;
}
</style>

<script>
import { listGoods, listTotalGoods, deleteGoods, checkGoods, queryProductDetails, lookReason } from '@/api/goodsManage'
import store from '@/store'
import BackToTop from '@/components/BackToTop'
import weishenhe from '@/assets/image/weishenhe.png'
import shenhezhong from '@/assets/image/shenhezhong.png'
import yitongguo from '@/assets/image/yitongguo.png'
import weitongguo from '@/assets/image/weitongguo.png'

export default {
  name: 'GoodsList',
  components: { BackToTop },
  data() {
    return {
      lowRepertory: 0,
      highRepertory: 0,
      list: [],
      describe: '',
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        goodsSn: undefined,
        name: undefined,
        mechanismId: store.getters.mechanismId,
        sort: '+id'
      },
      listQuery_1: {
        mechanismId: store.getters.mechanismId
      },
      productVisible: false,
      describeVisible: false,
      productForm: [],
      goodsCheck: [],
      descDetail: '',
      descDialogVisible: false,
      downloadLoading: false,
      mechanismId: '',
      weishenhe: weishenhe,
      shenhezhong: shenhezhong,
      yitongguo: yitongguo,
      weitongguo: weitongguo
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      listGoods(this.listQuery)
        .then(response => {
          this.list = response.data.data.items
          this.total = response.data.data.total
          this.listLoading = false
        })
        .catch(() => {
          this.list = []
          this.total = 0
          this.listLoading = false
        })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleSizeChange(val) {
      this.listQuery.limit = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getList()
    },
    handleCreate() {
      this.$router.push({ path: '/goodsManage/create' })
    },
    handleUpdate(row) {
      this.$router.push({ path: '/goodsManage/edit', query: { id: row.id }})
    },
    showDesc(desc) {
      this.descDetail = desc
      this.descDialogVisible = true
    },
    showRepetory(id) {
      queryProductDetails({ goodId: id }).then(response => {
        this.productForm = response.data.data.product
      })
      this.productVisible = true
    },
    handleDelete(row) {
      this.$confirm('此操作删除该信息, 是否继续?', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteGoods(row).then(response => {
          this.$notify({
            title: '成功',
            message: '删除成功',
            type: 'success',
            duration: 2000
          })
          const index = this.list.indexOf(row)
          this.list.splice(index, 1)
        })
      })
    },
    handleCheck(row) {
      row.mechanismId = this.listQuery_1.mechanismId
      this.$confirm('是否提交审核?', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        checkGoods(row).then(response => {
          this.$notify({
            title: '成功',
            message: '提交审核成功',
            type: 'success',
            duration: 2000
          })
          this.getList()
        })
      })
    },
    lookReason(row) {
      lookReason(row.id).then(response => {
        this.goodsCheck = response.data.data.goodsCheck
      })
      this.describeVisible = true
    },
    handleDownload() {
      import('@/vendor/Export2Excel').then(excel => {
        listTotalGoods(this.listQuery).then(response => {
          const tHeader = [
            '服务项目ID',
            '服务项目编号',
            '名称',
            '专柜价格',
            '积分价格',
            '是否新品',
            '是否在售',
            '审核状态',
            '宣传图片列表',
            '服务项目介绍',
            '详细介绍',
            '服务项目图片',
            '服务项目单位',
            '关键字'
          ]
          const filterVal = [
            'id',
            'goodsSn',
            'name',
            'counterPrice',
            'retailPrice',
            'isNew',
            'isOnSale',
            'isCheck',
            'gallery',
            'brief',
            'desc',
            'picUrl',
            'unit',
            'keywords'
          ]
          const list = response.data.data.items
          for (var i = 0; i < list.length; i++) {
            if (list[i].isNew === true) list[i].isNew = '新品'
            else if (list[i].isNew === false) list[i].isNew = '非新品'

            if (list[i].isOnSale === true) list[i].isOnSale = '在售'
            else if (list[i].isOnSale === false) list[i].isOnSale = '未售'

            if (list[i].isCheck === 0) list[i].isCheck = '未审核'
            else if (list[i].isCheck === 1) list[i].isCheck = '审核中'
            else if (list[i].isCheck === 2) list[i].isCheck = '已通过'
            else if (list[i].isCheck === 3) list[i].isCheck = '未通过'
            else list[i].isCheck = '未知状态'
          }
          excel.export_json_to_excel2(tHeader, list, filterVal, '服务项目信息')
          this.downloadLoading = false
        })
      })
    }
  }
}
</script>